﻿Type.registerNamespace("VisualCron");

VisualCron.LoadManager = function () {

    VisualCron.LoadManager.initializeBase(this);
    this._currentExpandingCount = 0;
    this._jobGridToCall = [];
    this._stubUpdateSelector = '#stubUpdate';
    this._taskGridToCall = [];
    this._updateInProgress = false;
}

VisualCron.LoadManager.prototype = {
    // public methods

    AddNodeExpanding: function (node) {
        this._currentExpandingCount++;
    },

    AddTaskGridToCall: function (serverId, jobId, btn) {
        if (btn) {
            var obj = new Object();
            obj.serverId = serverId;
            obj.jobId = jobId;
            obj.btn = btn;

            this._taskGridToCall.push(obj);

            if (!this._updateInProgress) {
                this._callTaskGrid();
            }
        }
    },

    OnAsyncRequestFinished: function () {
        //console.log('OnAsyncRequestFinished. JobGridToCall: ' + this._jobGridToCall.length + ' TaskGridToCall: ' + this._taskGridToCall.length);
        this._updateInProgress = false;

        // we try to update Job grid if necessary, if no job updates in queue - try to update task grid
        if (!this._callJobGrid()) {
            this._callTaskGrid();
        }
    },

    OnNodePopulated: function (node) {
        this._currentExpandingCount--;

        //console.log('OnNodePopulated ' + this._currentExpandingCount);
        var btn = $('#btnJobGridSetVisible', node._element);

        // we save a button which will cause async postback for newly recieved update panel
        this._jobGridToCall.push(btn);

        // and call stub update in order to make newly added update panel work (it won't work otherwise)
        // we only do this if no async action is already in progress
        if (!this._updateInProgress && (this._currentExpandingCount == 0)) {
            this.StubUpdatePanel();
        }

    },

    StubUpdatePanel: function () {
        //console.log('Call StubUpdatePanel');
        $(this._stubUpdateSelector).click();
        this._updateInProgress = true;
    },

    _callJobGrid: function () {
        var btn = this._jobGridToCall.shift();

        // here we make an async postback for newly added update panel - it should work now
        if (btn) {
            btn.click();
            this._updateInProgress = true;
            return true;
        }

        return false;
    },

    _callTaskGrid: function () {
        var obj = this._taskGridToCall.shift();

        if (obj) {
            $("#hdnJobId").val(obj.jobId);
            $("#hdnUserServerId").val(obj.serverId);

            obj.btn.click();
            this._updateInProgress = true;
            return true;
        }

        return false;
    },

}

VisualCron.LoadManager.registerClass('VisualCron.LoadManager', Sys.Component);

// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager
// that this is the end of the script.
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
